package com.tanran.system;

import com.sun.jna.platform.win32.Shell32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * 封装好的user32
 */

public class AppTest {
    public static String QQ = "909604002";
    public static String NAME = "乐乐";
    public static String MESSAGE = "abc" + new Date().getTime();

    public static void main(String[] args) throws InterruptedException, UnsupportedEncodingException {
        System.setProperty("jna.encoding","GBK");
        User32 user32 = User32.INSTANCE;
        Shell32 shell32 = Shell32.INSTANCE;
        WinDef.HWND hwnd=null;
        shell32.ShellExecute(new WinDef.HWND()
                , "open", "tencent://message/?uin=" + QQ, ""
                , null, 1);

        while(true){
             hwnd = user32.FindWindow("TXGuiFoundation", NAME);
            Thread.sleep(5000);
            if(hwnd!=null){
                break;
            }
        }
        //置焦点
        boolean flag = user32.SetForegroundWindow(hwnd);
        System.out.println("是否成功置焦点" + flag);
        //保持窗口在前
        flag = user32.SetWindowPos(hwnd, hwnd, 0, 0, 0, 0, 3);
        System.out.println("是否成功保持窗口在前" + flag);
        //准备投递消息
        byte[] bs = MESSAGE.getBytes("GBK");
        for (byte b : bs) {
            WinDef.WPARAM wparam = new WinDef.WPARAM();
            wparam.setValue(b);
            WinDef.LPARAM lparam = new WinDef.LPARAM();
            lparam.setValue(0);
            user32.PostMessage(hwnd, 258, wparam, lparam);
        }
        WinDef.WPARAM wparam = new WinDef.WPARAM();
        wparam.setValue(13);
        WinDef.LPARAM lparam = new WinDef.LPARAM();
        lparam.setValue(0);
        //回车键
        user32.PostMessage(hwnd, 256, wparam, lparam);
        //取消窗口置顶
        user32.SetWindowPos(hwnd, hwnd, 0, 0, 0, 0, 3);
        //关闭窗口
        user32.CloseWindow(hwnd);
    }

}


